package com.tarena.test.elasticsearch;

import org.apache.http.HttpHost;

import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;

public class ESIndexTestRun {
    //使用@Before构建一个使用的客户端对象
    private RestHighLevelClient client;
    @Before
    public void initClient(){
        HttpHost httpHost=new HttpHost("localhost",9200,"http");
        RestClientBuilder builder= RestClient.builder(httpHost);
        client=new RestHighLevelClient(builder);
    }
    //创建一个索引
    @Test
    public void createIndex() throws IOException {
        //1. 创建一个请求request对象 指定一个索引名称;索引名称必须是小写
        CreateIndexRequest request=new CreateIndexRequest("test01");
        //2. 使用client创建这个索引 后面的是默认的请求其他数据比如请求头
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        //3. 可以获返回结果,指导是否创建成功
        System.out.println(createIndexResponse.isAcknowledged());
    }
    //指定分片创建索引
    @Test
    public void createIndexWithShards() throws IOException {
        //1. 创建一个请求request对象 指定一个索引名称;索引名称必须是小写
        CreateIndexRequest request=new CreateIndexRequest("test02");
        //1.1 设定分片
        request.settings(Settings.builder().put("number_of_shards",1).put("number_of_replicas",0));
        //2. 使用client创建这个索引 后面的是默认的请求其他数据比如请求头
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        //3. 可以获返回结果,指导是否创建成功
        System.out.println(createIndexResponse.isAcknowledged());
    }
    //建立映射的索引
    /*
    映射 json
        {
          "properties": {
            "name": {
              "type": "text",
              "analyzer": "ik_max_word",
              "search_analyzer": "ik_smart"
            },
            "student_no":{
                "type":"text"
            },
            "birthday":{
                "type": "date"
            },
            "location":{
                "type":"geo_point"
            },
            "gender":{
                "type":"keyword"
            }
          }
        }
     */
    @Test
    public void createIndexWithMapping() throws IOException {
        //1. 创建一个请求request对象 指定一个索引名称;索引名称必须是小写
        CreateIndexRequest request=new CreateIndexRequest("test03");
        //1.1 设定分片
        request.settings(Settings.builder().put("number_of_shards",1).put("number_of_replicas",0));
        //1.2 设置映射
        request.mapping("{\"properties\":{\"name\":{\"type\":\"text\",\"analyzer\":\"ik_max_word\",\"search_analyzer\":\"ik_smart\"},\"student_no\":{\"type\":\"text\"},\"birthday\":{\"type\":\"date\"},\"location\":{\"type\":\"geo_point\"}}}", XContentType.JSON);
        //2. 使用client创建这个索引 后面的是默认的请求其他数据比如请求头
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        //3. 可以获返回结果,指导是否创建成功
        System.out.println(createIndexResponse.isAcknowledged());
    }

    //删除某个索引
    @Test
    public void deleteIndex() throws IOException {
        DeleteIndexRequest request=new DeleteIndexRequest("test01");
        client.indices().delete(request,RequestOptions.DEFAULT);
    }
    //获取索引
    @Test
    public void getIndex() throws IOException {
        GetIndexRequest request=new GetIndexRequest("test03" +
                "");
        //1. 判断索引存在
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println("索引是否存在:"+exists);
        //2. 拿到索引元数据(描述索引的数据 比如几个分片 几个副本 mapping是什么)
        GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT);
        Map<String, MappingMetaData> mappings = getIndexResponse.getMappings();
        for (Map.Entry<String, MappingMetaData> entry : mappings.entrySet()) {
            String indexName = entry.getKey();
            Map<String, Object> metadata = entry.getValue().sourceAsMap(); // 获取映射元数据
            System.out.println("Index: " + indexName);
            System.out.println("Mappings: " + metadata);
        }
        System.out.println(getIndexResponse.getSettings());
        System.out.println(getIndexResponse.getAliases());
        System.out.println(getIndexResponse.getIndices());
    }
}
